<?php

defined('BASEPATH') or exit('No direct script access allowed');

/**
 * Description of Users
 *
 * @author doufu <442958506@qq.com>
 */
class Diliverier extends REST_Diliveriers_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('api/Agent_model', 'Agent');
		$this->load->model('api/Order_model', 'Order');
		$this->load->library('form_validation');
	}

	/**
	 * 催单
	 */
	public function reminder() {
		$data = array();
		$where = array(
			'o.address_id' => $this->identity ['address_id'],
			'o.reminder' => parent::STATUS_ACTIVE,
			'o.status >' => parent::PAY_STATUS_UNPAY,
			'o.status <' => parent::PAY_STATUS_SIGNED
		);
		if (!$this->isProvider()) {
			$where ['o.agent_id'] = $this->identity ['uid'];
		}
		$data ['data'] = $this->Order->getOrders($where, $this->page);
		$this->renderJson(11100, $data);
	}

	/**
	 * 处理催单
	 */
	public function resolve() {
		$this->form_validation->validateData(array(
			'orderId'
				), array(
			array(
				'orderId',
				'integer|greater_than[0]'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}
		$data = array(
			'reminder' => parent::STATUS_DELETE,
			'updated_at' => time()
		);
		$where = array(
			'id' => $this->form_validation->orderId,
			'status >' => parent::PAY_STATUS_UNPAY,
			'status <' => parent::PAY_STATUS_SIGNED,
			'address_id' => $this->identity ['address_id']
		);
		if (!$this->isProvider()) {
			$where ['agent_id'] = $this->identity ['uid'];
		}
		$result = $this->Order->update($data, $where);
		if ($result) {
			$this->Order->log($where ['id'], 20, '', 2);
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	/**
	 * 已接单
	 */
	public function received() {
		$data = array();
		$where = array(
			'o.address_id' => $this->identity ['address_id'],
			'o.status' => array(
				parent::PAY_STATUS_PAYED,
				parent::PAY_STATUS_CASH,
				parent::PAY_STATUS_WAIT_DELIVER
			),
			'o.agent_id' => $this->identity ['uid']
		);
		$data ['data'] = $this->Order->getOrders($where, $this->page);
		$this->renderJson(11100, $data);
	}

	/**
	 * 确认发货
	 */
	public function confirm_dilivery() {
		$this->form_validation->validateData(array(
			'orderId'
				), array(
			array(
				'orderId',
				'integer|greater_than[0]'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}
		$data = array(
			'status' => parent::PAY_STATUS_DELIVERING,
			'updated_at' => time()
		);
		$where = array(
			'id' => $this->form_validation->orderId,
			'status' => parent::PAY_STATUS_WAIT_DELIVER,
			'address_id' => $this->identity ['address_id'],
			'agent_id =' => $this->identity ['uid']
		);
		$result = $this->Order->update($data, $where);
		if ($result) {
			$this->Order->log($where ['id'], $data ['status'], '', 2);
			$this->renderJson(11100);
		}
		$this->renderJson(10054);
	}

	/**
	 * 发货中
	 */
	public function dilivering() {
		$data = array();
		$where = array(
			'o.address_id' => $this->identity ['address_id'],
			'o.status' => array(
				parent::PAY_STATUS_DELIVERING
			),
			'o.agent_id' => $this->identity ['uid']
		);
		$data ['data'] = $this->Order->getOrders($where, $this->page);
		$this->renderJson(11100, $data);
	}

	/**
	 * 确认签收
	 */
	public function sign_for() {
		$this->form_validation->validateData(array(
			'orderId',
			'code'
				), array(
			array(
				'orderId',
				'integer|greater_than[0]'
			),
			array(
				'code',
				'required'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}
		$data = array(
			'status' => parent::PAY_STATUS_SIGNED,
			'updated_at' => time()
		);
		$where = array(
			'id' => $this->form_validation->orderId,
			'status' => parent::PAY_STATUS_DELIVERING,
			'agent_id' => $this->identity ['uid'],
			'address_id' => $this->identity ['address_id'],
			'sign_for_code' => $this->form_validation->code
		);
		$result = $this->Order->update($data, $where);
		if ($result) {
			$order = $this->Order->get_row(array('id' => $where['id']));
			$this->Order->log($where ['id'], $data ['status'], '', 2);
			//更新售出数目
			$this->load->model('api/Product_model','Product');
			$this->Product->updateSoldOut($order['id']);
			//发送消息
			$this->load->library('JPush');
			$this->jpush->push(array($order['user_id']), 'cfg_user_notify_signed', $order, array('data' => $order['id']));
			$this->load->config('system_info');
			if ($order['pay_time'] < $data['updated_at'] - config_item('cfg_scheduled_overtime_o')) {
				$this->Order->update(array('overtime' => REST_Controller::STATUS_ACTIVE), array('id' => $order['id']));
				$this->jpush->push(array($order['agent_id']), 'cfg_agent_notify_overtime', $order, array('data' => $order['id']));
			}
			$this->renderJson(11100);
		}
		$this->renderJson(10058);
	}

	/**
	 * 已签收
	 */
	public function signed() {
		$data = array();
		$where = array(
			'o.address_id' => $this->identity ['address_id'],
			'o.status' => array(
				parent::PAY_STATUS_SIGNED
			),
			'o.agent_id' => $this->identity ['uid']
		);
		$data ['data'] = $this->Order->getOrders($where, $this->page);
		$this->renderJson(11100, $data);
	}

	/**
	 * 已完成
	 */
	public function finish() {
		$data = array();
		$where = array(
			'o.address_id' => $this->identity ['address_id'],
			'o.status' => array(
				parent::PAY_STATUS_FINISH,
				parent::PAY_STATUS_CLOSE,
				parent::PAY_STATUS_REFUNDING,
				parent::PAY_STATUS_REFUNDED
			),
			'o.agent_id' => $this->identity ['uid']
		);
		$data ['data'] = $this->Order->getOrders($where, $this->page);
		$this->renderJson(11100, $data);
	}

	/**
	 * 新订单
	 */
	public function new_order() {
		$data = array();
		$where = array(
			'o.address_id' => $this->identity ['address_id'],
			'o.status' => array(
				parent::PAY_STATUS_PAYED,
				parent::PAY_STATUS_CASH,
				parent::PAY_STATUS_WAIT_DELIVER
			),
			'o.agent_id' => 0
		);
		$data ['data'] = $this->Order->getOrders($where, $this->page);
		$this->renderJson(11100, $data);
	}

	/**
	 * 抢单
	 */
	public function grab() {
		$this->form_validation->validateData(array(
			'orderId'
				), array(
			array(
				'orderId',
				'integer|greater_than[0]'
			)
		));
		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}
		$data = array(
			'agent_id' => $this->identity ['uid'],
			'updated_at' => time()
		);
		$where = array(
			'id' => $this->form_validation->orderId,
			'status' => array(
				parent::PAY_STATUS_PAYED,
				parent::PAY_STATUS_CASH,
				parent::PAY_STATUS_WAIT_DELIVER
			),
			'address_id' => $this->identity ['address_id'],
			'agent_id' => 0
		);
		$result = $this->Order->update($data, $where);
		if ($result) {
			$order = $this->Order->get_row(array('id' => $where['id']));
			$short_desc = sprintf($this->lang->line("s10004"), $this->identity['nickname'], $this->identity['username']);
			$this->Order->log($order ['id'], $order ['status'], $short_desc, 2);
			//发送消息
			$this->load->library('JPush');
			$order['arrived_time'] = date('H:i', $order['pay_time'] + 30 * 60);
			$this->jpush->push(array($order['user_id']), 'cfg_user_notify_received', $order, array('data' => $order['id']));
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	/**
	 * 备货中
	 */
	public function ready() {
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$data = array();
		$where = array(
			'o.address_id' => $this->identity ['address_id'],
			'o.status' => array(
				parent::PAY_STATUS_PAYED,
				parent::PAY_STATUS_CASH
			)
		);
		$data ['data'] = $this->Order->getOrders($where, $this->page);
		$this->renderJson(11100, $data);
	}

	/**
	 * 备货
	 */
	public function wait_dilivery() {
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$this->form_validation->validateData(array(
			'orderId'
				), array(
			array(
				'orderId',
				'integer|greater_than[0]'
			)
		));
		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}
		$data = array(
			'status' => parent::PAY_STATUS_WAIT_DELIVER,
			'updated_at' => time()
		);
		$where = array(
			'id' => $this->form_validation->orderId,
			'status' => array(
				parent::PAY_STATUS_PAYED,
				parent::PAY_STATUS_CASH
			),
			'address_id' => $this->identity ['address_id']
		);
		$result = $this->Order->update($data, $where);
		if ($result) {
			$order = $this->Order->get_row(array('id' => $where['id']));
			if (!empty($order['agent_id'])) {
				//发送消息
				$this->load->library('JPush');
				$this->jpush->push(array($order['agent_id']), 'cfg_agent_notify_wait', $order, array('data' => $order['id']));
			}
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	/**
	 * 退款
	 */
	public function refund() {
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$this->form_validation->validateData(array(
			'orderId'
				), array(
			array(
				'orderId',
				'integer|greater_than[0]'
			)
		));
		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}
		$data = array(
			'status' => parent::PAY_STATUS_REFUNDING,
			'updated_at' => time()
		);
		$where = array(
			'id' => $this->form_validation->orderId,
			'status >' => parent::PAY_STATUS_UNPAY,
			'status <' => parent::PAY_STATUS_REFUNDING,
			'address_id' => $this->identity ['address_id']
		);
		$order = $this->Order->get_row($where); // 获取退款所需信息
		if (empty($order)) {
			$this->renderJson(11200);
		}

		$result = false;
		$time = time();
		switch ($order ['pay_method']) {
			case parent::PAY_METHOD_CASH :
				if ($order['status'] >= REST_Controller::PAY_STATUS_SIGNED) {
					$dataInsert = array(
						'user_id' => $order['user_id'],
						'agent_id' => $this->identity['uid'],
						'order_id' => $order['id'],
						'status' => REST_Controller::STATUS_DELETE,
						'in_status' => $order['status'],
						'op_user_id' => 0,
						'refund_time' => $time,
						'created_at' => $time,
						'updated_at' => $time
					);
					$this->load->model('api/Refund_model', 'Refund');
					$tmp = $this->Refund->insert($dataInsert);
					if($tmp) {
						$result = $this->Order->update($data, $where);
					}
				} else {
					$data ['status'] = parent::PAY_STATUS_CLOSE;
					$result = $this->Order->close($order['id']);
				}
				break;
			case parent::PAY_METHOD_ALIPY :
			case parent::PAY_METHOD_WECHAT :
				// 退款接口、待补充
				$dataInsert = array(
					'user_id' => $order['user_id'],
					'agent_id' => $this->identity['uid'],
					'order_id' => $order['id'],
					'status' => REST_Controller::STATUS_ACTIVE,
					'in_status' => $order['status'],
					'created_at' => $time,
					'updated_at' => $time
				);
				$this->load->model('api/Refund_model', 'Refund');
				$tmp = $this->Refund->insert($dataInsert);
				if($tmp) {
					$result = $this->Order->update($data, $where);
				}
				break;
			default:
				$this->renderJson(11200);
				break;
		}		
		if ($result) {
			//发送消息
			$this->load->library('JPush');
			$this->jpush->push(array($order['user_id']), 'cfg_user_notify_cancel', $order, array('data' => $order['id']));
			$this->jpush->push(array($order['agent_id']), 'cfg_agent_notify_cancel', $order, array('data' => $order['id']));
			$this->Order->log($where ['id'], $data ['status'], '', 3);
			$this->renderJson(11100);
		}
	}

	/**
	 * 所有订单
	 */
	public function orders() {
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$post = $this->input->post();
		$data = array();
		$where = array(
			'o.address_id' => $this->identity ['address_id'],
			'o.status >' => parent::PAY_STATUS_UNPAY
		);
		if (isset($post ['status']) && ($status = intval($post ['status'])) && $status > parent::PAY_STATUS_UNPAY) {
			$where ['o.status'] = $status . '';
		}
		if (isset($post ['min_pay_time']) && ($min_pay_time = intval($post ['min_pay_time']))) {
			$where ['o.pay_time >='] = $min_pay_time;
		}
		if (isset($post ['max_pay_time']) && $max_pay_time = intval($post ['max_pay_time'])) {
			$where ['o.pay_time <='] = $max_pay_time;
		}
		if (isset($post ['nickname']) && !empty($post ['nickname'])) {
			$where ['a.nickname like'] = "%{$post['nickname']}%";
		}
		$data ['data'] = $this->Order->getOrders($where, $this->page);
		$this->renderJson(11100, $data);
	}

	/**
	 * 补仓
	 */
	public function fillStock() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}

		$stock = $this->post('stock');
		if (empty($stock)) {
			$this->renderJson(10005);
		}

		$data = array();
		foreach ($stock as $v) {
			if (intval($v ['request_num']) < 1) {
				$this->renderJson(10050);
			}
			if(isset($v['id']) && isset($v['request_num'])) {
				$data [] = array(
					'product_id' => intval($v ['id']),
					'request_num' => intval($v ['request_num'])
				);
			} else {
				$this->renderJson(10005);
			}
		}
		$this->load->model('api/Stock_model', 'Stock');
		$result = $this->Stock->addStock($data, $this->identity ['uid']);
		if ($result === true) {
			$this->renderJson(11100);
		}

		$this->renderJson($result);
	}

	/**
	 * 编辑入库
	 */
	public function editInStock() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}

		$this->form_validation->validateData(array(
			'orderid'
				), array(
			array(
				'orderid',
				'required|integer'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$stock = $this->post('stock');
		if (empty($stock)) {
			$this->renderJson(10005);
		}

		$data = array();
		foreach ($stock as $v) {
			if (intval($v ['request_num']) < 1) {
				$this->renderJson(10050);
			}
			if(isset($v['id']) && isset($v['request_num'])) {
				$data [] = array(
					'product_id' => intval($v ['id']),
					'request_num' => intval($v ['request_num'])
				);
			} else {
				$this->renderJson(10005);
			}
		}

		$this->load->model('api/Stock_model', 'Stock');

		$result = $this->Stock->addWareStock($this->form_validation->orderid, $data, $this->identity);
		if ($result === true) {
			$this->renderJson(11100);
		}
		$this->renderJson($result);
	}

	/**
	 * 取得补货列表
	 */
	public function getFillStock() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$this->load->model('api/Stock_model', 'Stock');
		$data = array();
		$data ['data'] = $this->Stock->getStock($this->identity ['uid'], $this->page, '0');
		$this->renderJson(11100, $data);
	}

	/**
	 * 取得退仓列表
	 */
	public function getBackStock() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$this->load->model('api/Stock_model', 'Stock');
		$data = array();
		$data ['data'] = $this->Stock->getStock($this->identity ['uid'], $this->page, '1');
		$this->renderJson(11100, $data);
	}

	/**
	 * 退仓
	 */
	public function backStock() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}

		$stock = $this->post('stock');
		if (empty($stock)) {
			$this->renderJson(10005);
		}

		$data = array();
		foreach ($stock as $v) {
			if (intval($v ['request_num']) < 1) {
				$this->renderJson(10050);
			}

			$data [] = array(
				'product_id' => intval($v ['id']),
				'request_num' => intval($v ['request_num'])
			);
		}
		$this->load->model('api/Stock_model', 'Stock');
		$result = $this->Stock->backStock($data, $this->identity ['uid']);
		if ($result) {
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	/**
	 * 获取个人指数
	 */
	public function getPersonZS() {
		$this->load->model('api/Order_model', 'Order');
		$data ['data'] = $this->Order->PersonZS($this->identity ['uid']);
		$this->renderJson(11100, $data);
	}

	/**
	 * 获取店铺指数
	 */
	public function getShopZS() {
		$this->load->model('api/Order_model', 'Order');
		$data ['data'] = $this->Order->ShopZS($this->identity ['address_id']);
		$this->renderJson(11100, $data);
	}

	/**
	 * 获取店铺公告
	 */
	public function getNotice() {
		$this->load->model('api/Areas_model', 'Areas');
		$data ['data'] = $this->Areas->findAreas($this->identity ['address_id'][0], 'notice');
		$this->renderJson(11100, $data);
	}

	/**
	 * 添加店铺公告
	 */
	public function addNotice() {
		$this->form_validation->validateData(array(
			'content'
				), array(
			array(
				'content',
				'required'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$this->load->model('api/Areas_model', 'Areas');

		$data = array(
			'notice' => $this->form_validation->content
		);
		if ($this->Areas->updateAreas($data, $this->identity ['address_id'])) {
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	/**
	 * 
	 */
	public function getDiliver() {
		$this->load->model('api/Order_model', 'Order');
		$data ['data'] = $this->Order->PersonZS($this->identity ['uid']);
		$this->renderJson(11100, $data);
	}

	/**
	 * 获取补仓单列表（即总仓库商品列表）
	 */
	public function getStockAll() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$this->load->model('api/Stock_model', 'Stock');
		$data = array();
		$data ['data'] = $this->Stock->getStockAll($this->page, $this->identity ['uid']);

		$this->renderJson(11100, $data);
	}

	/**
	 * 取得该订单号下的商品列表
	 */
	public function getStockByNum() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}


		$this->form_validation->validateData(array(
			'order_num'
				), array(
			array(
				'order_num',
				'required'
			)
		));
		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$this->load->model('api/Stock_model', 'Stock');
		$data = array();
		$data ['data'] = $this->Stock->getStockByNum($this->page, $this->form_validation->order_num);
		$this->renderJson(11100, $data);
	}

	/**
	 * 取得网点管理员管理的仓库商品列表
	 */
	public function getStockByWD() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$this->load->model('api/Stock_model', 'Stock');
		$data = array();
		$data ['data'] = $this->Stock->getStockByWD($this->page, $this->identity ['uid']);

		$this->renderJson(11100, $data);
	}

	/**
	 * 取消补仓（退仓）订单申请
	 */
	public function cancelStock() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}


		$this->form_validation->validateData(array(
			'order_num'
				), array(
			array(
				'order_num',
				'required'
			)
		));
		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$this->load->model('api/Stock_model', 'Stock');
		$result = $this->Stock->cancelStock($this->form_validation->order_num);

		if ($result) {
			$this->renderJson(11100);
		}

		$this->renderJson(11203);
	}

	/**
	 * 添入预警库存
	 */
	public function addWarnStock() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}


		$this->form_validation->validateData(array(
			'product_id',
			'warn_number'
				), array(
			array(
				'product_id',
				'required'
			),
			array(
				'warn_number',
				'required'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$this->load->model('api/Stock_model', 'Stock');
		$result = $this->Stock->addWarnStock($this->form_validation->product_id, $this->form_validation->warn_number, $this->identity ['uid']);

		if ($result) {
			$this->renderJson(11100);
		}

		$this->renderJson(11203);
	}

	/**
	 * 取得预警库存列表
	 */
	public function getStockWarn() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}

		$this->load->model('api/Stock_model', 'Stock');
		$data = array();
		$data ['data'] = $this->Stock->getStockWarn($this->page, $this->identity ['uid']);

		$this->renderJson(11100, $data);
	}

	/**
	 * 取得达到库存预警值的数量
	 */
	public function getStockWarnNum() {
		$this->load->model('api/Stock_model', 'Stock');
		$data = array();
		$data ['data'] = $this->Stock->getStockWarnNum($this->identity ['uid']);
		$this->renderJson(11100, $data);
	}

	/**
	 * 商品下/上架
	 */
	public function stockDownOrUp() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}

		$this->form_validation->validateData(array(
			'product_id',
			'status'
				), array(
			array(
				'product_id',
				'required'
			),
			array(
				'status',
				'required'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$this->load->model('api/Stock_model', 'Stock');
		$result = $this->Stock->stockDownOrUp($this->form_validation->product_id, $this->identity ['uid'], $this->form_validation->status);

		if ($result) {
			$this->renderJson(11100);
		}

		$this->renderJson(11200);
	}

	//获取所有小哥
	public function audit() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$this->load->model('api/Agent_model', 'Agent');
		$data['data'] = $this->Agent->getDiliverierAll($this->identity['address_id'], $this->page);
		foreach ($data['data'] as &$v) {
			$v['img'] = empty($v['img']) ? '' : base_url($v['img']);
		}
		$this->renderJson(11100, $data);
	}

	public function pass() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$this->form_validation->validateData(array(
			'id', 'status'
				), array(
			array(
				'id',
				'required|greater_than[0]'
			),
			array(
				'status',
				'required|in_list[0,2]'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}
		$data = array(
			'status' => $this->form_validation->status,
			'updated_at' => time(),
		);
		$where = array(
			'id' => $this->form_validation->id,
			'status' => REST_Controller::STATUS_PROCESSING,
		);
		$this->load->model('api/Agent_model', 'Agent');
		$result = $this->Agent->update($data, $where);
		if ($result) {
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	public function store_status() {
		$this->load->model('api/Agent_model', 'Agent');
		$data['data'] = $this->Agent->assignment_status($this->identity['address_id'][0]);
		$this->renderJson(11100, $data);
	}

	public function set_store_status() {
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$this->form_validation->validateData(array(
			'status'
				), array(
			array(
				'status',
				'required|in_list[0,10]'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}
		$data = array(
			'status' => $this->form_validation->status,
			'updated_at' => time(),
		);
		$where = array(
			'id' => $this->identity['address_id'],
		);
		$this->load->model('api/Agent_model', 'Agent');
		$result = $this->Agent->updateAssignment($data, $where);
		if ($result) {
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	/**
	 * 获取营业统计(按照时间段进行统计)
	 */
	public function getShopReport() {
		$this->form_validation->validateData(array(
			'timeStart',
			'timeEnd'
				), array(
			array(
				'timeStart',
				'required'
			),
			array(
				'timeEnd',
				'required'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$this->load->model('api/Order_model', 'Order');
		$data ['data'] = $this->Order->getShopReport($this->identity ['address_id'], $this->form_validation->timeStart, $this->form_validation->timeEnd);
		$this->renderJson(11100, $data);

	}
	
	
	/**
	 * 取得库存列表（上下架，按照商品上架的前面，下架的后面）
	 */
	public function getStockOrderStatus()
	{
		// 判断是否网店管理员
		if (!$this->isProvider()) {
			$this->renderJson(10056);
		}
		$this->load->model('api/Stock_model', 'Stock');
		$data = array();
		$data ['data'] = $this->Stock->getStockOrderStatus($this->page, $this->identity ['uid']);
		$this->renderJson(11100, $data);
	}

}
